package com.qf.homework.day38.dao;


import com.qf.homework.day38.pojo.User;
import com.qf.homework.day38.utils.DBUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    public int usernameExist(String username) throws SQLException {
        int count = 0;
        String sql = "select count(*)from user where username=?";
        PreparedStatement ps = DBUtil.getConnection().prepareStatement(sql);
        ps.setString(1,username);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()){count=resultSet.getInt(1);}
        return count;
    }

    public int addUser(User user) throws SQLException {
        String sql="insert into user(username,password,address,hobbies,class_id) values(?,?,?,?,?)";
        PreparedStatement ps = DBUtil.getConnection().prepareStatement(sql);
        ps.setString(1,user.getUsername());
        ps.setString(2,user.getPassword());
        ps.setString(3,user.getAddress());
        ps.setString(4,user.getHobbies());
        ps.setInt(5,2);
        return ps.executeUpdate();
    }
}
